package com.draw.server;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.draw.core.Color;
import com.draw.core.Word;
import com.draw.server.hibernate.HibernateUtil;
import com.draw.server.service.ColorService;
import com.draw.server.service.WordService;

public class Loader {
	public Loader() {
		WordService ws = new WordService();
		List<Word> lw = ws.getAll();
		Word w = new Word();
		w.setLevel(1);
		w.setWord("COUCOU");
		if (lw == null || !lw.contains(w)) {
			ws.addWord(w);
		}
		w = new Word();
		w.setLevel(2);
		w.setWord("PIANO");
		if (lw == null || !lw.contains(w)) {
			ws.addWord(w);
		}
		w = new Word();
		w.setLevel(3);
		w.setWord("GAGA");
		if (lw == null || !lw.contains(w)) {
			ws.addWord(w);
		}

		ColorService cs = new ColorService();
		Color color[] = new Color[5];
		color[0] = new Color(java.awt.Color.BLACK);
		color[1] = new Color(java.awt.Color.WHITE);
		color[2] = new Color(java.awt.Color.BLUE);
		color[3] = new Color(java.awt.Color.RED);
		color[4] = new Color(java.awt.Color.YELLOW);

		List<Color> lc = cs.getAllColor();
		Session session = HibernateUtil.getSession();
		Transaction tx = session.beginTransaction();
		for (int i = 0; i < color.length; i++) {
			if ( lc == null || ! lc.contains(color[i]) ) {
				session.save(color[i]);
			}
		}
		tx.commit();
	}
}
